package com.vivo.tasks.vivofx;


import com.thinkgem.jeesite.common.persistence.Page;
import com.vivo.common.DatabaseContextHolder;
import com.vivo.dealer.entity.ViDeaWarehousesn;
import com.vivo.dealer.service.ViDeaWarehousesnService;
import com.vivo.fx.dealer.entity.FxDeaWarehousesn;
import com.vivo.fx.dealer.service.FxDeaWarehousesnService;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
public class TaskDeaWarehouseSn {

    @Autowired
    private FxDeaWarehousesnService fxDeaWarehousesnService;

    @Autowired
    private ViDeaWarehousesnService viDeaWarehousesnService;


    private void SyncData() {
        syncLastUpdData("ah");
        syncLastUpdData("js");
    }


    private void syncLastUpdData(String area) {
        System.out.println("get ware house sn info from " + area + " start ...");
        Calendar cal = Calendar.getInstance();
        Date startDate = null;
        Date endDate = new Date();

        // GetLastItem
        ViDeaWarehousesn lastItem = new ViDeaWarehousesn();
        lastItem.setTableZone(area);
        lastItem = viDeaWarehousesnService.getLastChangeItem(lastItem);

        // 如果没有，则需要初始化数据, 获取上个月数据
        if (lastItem == null) {
            cal.setTime(endDate);
            cal.add(Calendar.DAY_OF_MONTH, -10);
            startDate = cal.getTime();
        } else {
            startDate = lastItem.getLastChangeDate();
        }

        int pageStart = 1;
        Page<FxDeaWarehousesn> page = new Page<FxDeaWarehousesn>(pageStart, 5000);
        FxDeaWarehousesn fxDeaWarehousesn = new FxDeaWarehousesn();
        fxDeaWarehousesn.setBeginLastchangedate(startDate);
        fxDeaWarehousesn.setEndLastchangedate(endDate);

        try {
            Helper.changeDB(area);
            Page<FxDeaWarehousesn> pageRes = fxDeaWarehousesnService.findPage(page, fxDeaWarehousesn);
            System.out.println("page count is : " + pageRes.getTotalPage());
            DatabaseContextHolder.setMainDb();

            saveSnList(pageRes.getList(), area);

            for (int i = pageStart + 1; i <= pageRes.getTotalPage(); i++) {
                System.out.println("cur page is :" + pageRes.getPageNo() + " and pageSize is :" + pageRes.getPageSize());

                page.setPageNo(i);

                Helper.changeDB(area);
                pageRes = fxDeaWarehousesnService.findPage(page, fxDeaWarehousesn);
                DatabaseContextHolder.setMainDb();

                // 保存数据
                saveSnList(pageRes.getList(), area);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseContextHolder.setMainDb();
        }

        System.out.println("get ware house sn info from " + area + " end !");
    }

    private void saveSnList(List<FxDeaWarehousesn> snList, String area) {
        for (FxDeaWarehousesn fxSn : snList) {
            ViDeaWarehousesn vs = new ViDeaWarehousesn();
            vs.setId(fxSn.getSnid());
            vs.setTableZone(area);
            vs.setIsTaskInsert(true);
            vs.setWaresId(fxSn.getWaresid());
            vs.setWarehouseId(fxSn.getWarehouseid());
            vs.setSnNumber(fxSn.getSnnumber());
            vs.setSnState(fxSn.getSnstate());
            vs.setInwarehouseDate(fxSn.getInwarehousedate());
            vs.setExchangeSourceWarehouseId(fxSn.getExchangesourcewarehouseid());
            vs.setExchangeSourceState(fxSn.getExchangesourcestate());
            vs.setExchangeSourcesnId(fxSn.getExchangesourcesnid());
            vs.setLastChangeDate(fxSn.getLastchangedate());
            vs.setChangeOrderType(fxSn.getChangeordertype());
            vs.setChangeOrderId(fxSn.getChangorderid());
            vs.setContractMachineKind(fxSn.getContractmachinekind());
            vs.setIsModelMachine(fxSn.getIsmodelmachine());
            vs.setAgentCode(fxSn.getAgentcode());
            vs.setDispatchImeiId(fxSn.getDispatchimeiid());
            vs.setElectronicCardId(fxSn.getElectroniccardid());
            vs.setIsBadMachine(fxSn.getIsbadmachine());

            try {
                viDeaWarehousesnService.save(vs);
            } catch (Exception e){
//                e.printStackTrace();
                System.out.println("数据已经存在：" + vs.getId());

                vs = viDeaWarehousesnService.getById(vs);
                // 已经存在，需要进行更新即可
                if (vs != null) {
                    vs.setTableZone(area);

                    vs.setWaresId(fxSn.getWaresid());
                    vs.setWarehouseId(fxSn.getWarehouseid());
                    vs.setSnNumber(fxSn.getSnnumber());
                    vs.setSnState(fxSn.getSnstate());
                    vs.setInwarehouseDate(fxSn.getInwarehousedate());
                    vs.setExchangeSourceWarehouseId(fxSn.getExchangesourcewarehouseid());
                    vs.setExchangeSourceState(fxSn.getExchangesourcestate());
                    vs.setExchangeSourcesnId(fxSn.getExchangesourcesnid());
                    vs.setLastChangeDate(fxSn.getLastchangedate());
                    vs.setChangeOrderType(fxSn.getChangeordertype());
                    vs.setChangeOrderId(fxSn.getChangorderid());
                    vs.setContractMachineKind(fxSn.getContractmachinekind());
                    vs.setIsModelMachine(fxSn.getIsmodelmachine());
                    vs.setAgentCode(fxSn.getAgentcode());
                    vs.setDispatchImeiId(fxSn.getDispatchimeiid());
                    vs.setElectronicCardId(fxSn.getElectroniccardid());
                    vs.setIsBadMachine(fxSn.getIsbadmachine());

                    try {
                        viDeaWarehousesnService.save(vs);
                    } catch (Exception ex) {
                        System.out.println("数据：" + vs.getId() + "修改失败！" + ex.getMessage());
                    }

                }
            }
        }
    }
}
